package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.MchntOrderDetailMapper;
import cc.rengu.oltp.service.common.entity.MchntOrderDetail;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户订单明细表操作接口实现类
 * Created by 王成 on 2020/10/16
 */
public class MchntOrderDetailMapperImpl implements MchntOrderDetailMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public MchntOrderDetailMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public int insertMchntOrderDetail(MchntOrderDetail mchntOrderDetail) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertMchntOrderDetail");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntOrderDetail, null);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("登记商户订单明细失败,instId:<{}>,mchntOrderId:<{}>,mchntSubOrderId:<{}>,mchntNo:<{}>,sysSeqNum:<{}>,iReturnCode:<{}>",
                    mchntOrderDetail.getInstId(), mchntOrderDetail.getMchntOrderId(), mchntOrderDetail.getMchntSubOrderId(), mchntOrderDetail.getMchntNo(), mchntOrderDetail.getSysSeqNum(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateMchntOrderDetailByPrimaryKey(MchntOrderDetail mchntOrderDetail) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMchntOrderDetailByPrimaryKey");
        mchntOrderDetail.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntOrderDetail, null);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据主键更新商户订单明细失败,instId:<{}>,mchntOrderId:<{}>,mchntSubOrderId:<{}>,mchntNo:<{}>,iReturnCode:<{}>",
                    mchntOrderDetail.getInstId(), mchntOrderDetail.getMchntOrderId(), mchntOrderDetail.getMchntSubOrderId(), mchntOrderDetail.getMchntNo(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public MchntOrderDetail selectMchntOrderDetailByPrimaryKey(String instId, String mchntNo, String mchntOrderId, String mchntSubOrderId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntOrderDetailByPrimaryKey");
        MchntOrderDetail mchntOrderDetail = new MchntOrderDetail();
        mchntOrderDetail.setInstId(instId);
        mchntOrderDetail.setMchntNo(mchntNo);
        mchntOrderDetail.setMchntOrderId(mchntOrderId);
        mchntOrderDetail.setMchntSubOrderId(mchntSubOrderId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntOrderDetail, mchntOrderDetail);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.warn("根据主键查询商户订单明细失败,instId:<{}>,mchntOrderId:<{}>,mchntSubOrderId:<{}>,mchntNo:<{}>,iReturnCode:<{}>",
                    instId, mchntOrderId, mchntSubOrderId, mchntNo, iReturnCode);
            return null;
        }
        return mchntOrderDetail;
    }

    @Override
    public List<MchntOrderDetail> selectMchntOrderDetailBySysSeqNum(String instId, String sysSeqNum) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntOrderDetailBySysSeqNum");
        MchntOrderDetail mchntOrderDetail = new MchntOrderDetail();
        mchntOrderDetail.setInstId(instId);
        mchntOrderDetail.setRefundSeqNum(sysSeqNum);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntOrderDetail, MchntOrderDetail.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.warn("根据系统流水号查询商户订单明细失败,instId:<{}>,sysSeqNum:<{}>", instId, sysSeqNum);
            return null;
        }
        return objectList.stream().map(item -> (MchntOrderDetail) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntOrderDetail> selectMchntOrderDetailByRefundSeqNum(String instId, String refundSeqNum) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntOrderDetailByRefundSeqNum");
        MchntOrderDetail mchntOrderDetail = new MchntOrderDetail();
        mchntOrderDetail.setInstId(instId);
        mchntOrderDetail.setRefundSeqNum(refundSeqNum);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntOrderDetail, MchntOrderDetail.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.warn("根据退款流水号查询商户订单明细失败,instId:<{}>,refundSeqNum:<{}>", instId, refundSeqNum);
            return null;
        }
        return objectList.stream().map(item -> (MchntOrderDetail) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntOrderDetail> selectMchntOrderDetailByMchntOrderId(String instId, String mchntNo, String mchntOrderId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntOrderDetailByMchntOrderId");
        MchntOrderDetail mchntOrderDetail = new MchntOrderDetail();
        mchntOrderDetail.setInstId(instId);
        mchntOrderDetail.setMchntNo(mchntNo);
        mchntOrderDetail.setMchntOrderId(mchntOrderId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntOrderDetail, MchntOrderDetail.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.warn("根据商户号及商户订单号查询商户订单明细失败,instId:<{}>,mchntNo:<{}>,mchntOrderId:<{}>", instId, mchntNo, mchntOrderId);
            return null;
        }
        return objectList.stream().map(item -> (MchntOrderDetail) item).collect(Collectors.toList());
    }
}
